import requests


# 定义目标URL
url2 = "http://localhost:9090/user/login"  # 替换为实际API地址
# 准备请求头
headers = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    # "Authorization": "Bearer YOUR_ACCESS_TOKEN"  # 替换为实际的认证令牌
}

payload = {
    "username": "wyn3",
    "password": "wyn123456",
}
try:
    # 发送POST请求
    response = requests.post(
        url2,
        json=payload,  # 自动序列化为JSON并设置Content-Type
        headers=headers
    )
    # 检查HTTP状态码
    if response.status_code == 200:
        print("测试登录接口成功")
        print("请求成功！")
        print("响应内容：", response.json())  # 解析JSON响应
        token=response.json()["data"]

        print("登录数据返回成功")
        print(token)


        # tokenName=response.json()["data"]["tokenName"]
        # tokenValue=response.json()["data"]["tokenValue"]
    else:
        print(f"请求失败！状态码：{response.status_code}")
        print("错误信息：", response.text)

except requests.exceptions.RequestException as e:
    print("请求异常：", e)
except ValueError:  # JSON解析异常
    print("响应不是有效的JSON格式")

# 定义目标URL
url1 = "http://localhost:9090/order/All/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
# 准备请求头
response = requests.get(url1)
print(response.text)


url3 = "http://localhost:9090/order/goods/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
# 准备请求头
response = requests.get(url3)
print(response.text)


url4 = "http://localhost:9090/order/goods/百香果/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
# 准备请求头
response = requests.get(url4)
print(response.text)


# searchGoodsByKeys/{keys}/{pageNum}

url5 = "http://localhost:9090/order/searchGoodsByKeys/水蜜桃/1"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
# 准备请求头
response = requests.get(url5)
print(response.text)


# selectById/{id}

url6 = "http://localhost:9090/order/selectById/69"  # 替换为实际API地址
headers1 = {
    "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
# 准备请求头
response = requests.get(url6)
print(response.text)



print(token)
url7 = "http://localhost:9090/order/search/goods/1"  # 替换为实际API地址
headers1 = {
    # "Content-Type": "application/json",  # 声明JSON内容类型
    "Authorization": token  # 替换为实际的认证令牌
}
# 准备请求头
response = requests.get(url7)
print(response.text)